import java.util.Scanner;

/**
 * @author gao
 * @date 2025/8/17 21:35
 */

public class Solution {

    /**
     * 使用动态规划方法求解最大子数组问题
     * @param nums 输入的整数数组
     * @return 返回最大子数组的和
     */
    public int maxSubArray(int[] nums) {
        int n = nums.length;
        int[] dp = new int[n + 1];
        // 初始化结果为最小整数值，确保任何子数组和都会大于它
        int ret = Integer.MIN_VALUE;
        for (int i = 1; i <= n; i++) {
            dp[i] = Math.max(nums[i - 1], dp[i - 1] + nums[i - 1]);
            // 更新全局最大值
            ret = Math.max(ret, dp[i]);
        }
        return ret;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            int[] nums = new int[n];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = in.nextInt();
            }
            Solution solution = new Solution();
            System.out.println(solution.maxSubArray(nums));
        }
    }
}